<?php 
class Mtarget extends CI_Model {

	function Mtarget(){
		parent::__construct();
	}
    
    function getTargetUnapprove($kpi_code,$month){
        $query =$this->db->query(   "   SELECT
                                        kpi.kpi_description,tar.target_id,tar.nik, usr.user_name, substr(tar.kpi_month,1,4) as year,substr(tar.kpi_month,5) as month, tar.kpi_target, tar.kpi_code
                                        FROM 
                                        t_trx_target_monthly tar
                                        LEFT JOIN t_mtr_user usr        
                                        ON usr.nik=tar.nik  
                                        INNER JOIN t_mtr_kpi kpi on
                                        kpi.kpi_code=tar.kpi_code
                                        
                                        WHERE tar.istatus=0 
                                        and tar.kpi_month='$month' and tar.kpi_code='$kpi_code'
                                        order by tar.kpi_code,usr.user_name                                      
                                    "
                                );
        $arr=$query->result_array();
        return $arr;
    }
    
    function getTargetAllocation($month,$user_group){
        $query =$this->db->query(   "
                                        SELECT  COALESCE(kpi.kpi_parent,kpi.kpi_code) as parent, kpi.kpi_code, alc.target, alc.allocated_month FROM t_mtr_kpi kpi inner join t_trx_target_allocation alc on COALESCE(kpi.kpi_parent,kpi.kpi_code)=alc.kpi_code where user_group_id=$user_group AND alc.nik='".$this->session->userdata('username')."' and alc.allocated_month='$month';
        
                                    "
                                );
        return json_encode($query->result());
    }
    
    function getUserTarget($user_group,$month){
        $query=$this->db->query (
                                    "SELECT distinct kpi.kpi_code, kpi.kpi_description, usr.user_id, usr.user_name, coalesce(tar.kpi_target,0) as kpi_target, kpi.remark From t_trx_target_monthly tar
                                   INNER join  t_mtr_user usr ON usr.nik=tar.nik  INNER JOIN t_mtr_kpi kpi on kpi.kpi_code=tar.kpi_code  where usr.user_group_id=".$user_group."
                                      and usr.reporting_to='".$this->session->userdata('username')."' and tar.kpi_month='".$month."'
                                    ORDER BY kpi.kpi_code,usr.user_name"
                                    ); 
        if($query->num_rows()){
           
            $return['status']='Update';
            $return['data']=$query->result();            
        } else {
            $query=$this->db->query (
                                    "SELECT kpi.kpi_code, kpi.kpi_description, usr.user_id, usr.user_name, 0 as kpi_target, kpi.remark From t_mtr_user usr
                                    LEFT JOIN t_mtr_kpi kpi on usr.user_group_id=kpi.user_group_id where usr.user_group_id=".$user_group."
                                    and usr.reporting_to='".$this->session->userdata('username')."'
                                    ORDER BY kpi.kpi_code,usr.user_name "
                                );   
            $return['status']='New';
            $return['data']=$query->result();          
        }
        return json_encode($return);
    }
    
    function setTarget($data){        
        $dblokal=$this->load->database("default", TRUE);
        $dblokal->insert_batch('t_trx_target_monthly', $data); 
    }
    
    function updateTarget($data){        
        $dblokal=$this->load->database("default", TRUE);
        $dblokal->update_batch('t_trx_target_monthly', $data,array('kpi_code'=>$data['kpi_code'],'nik'=>$data['nik'],'kpi_month'=>$data['kpi_month'])); 
        $dblokal->update_batch('t_trx_target_monthly', array('istatus'=>0),array('kpi_code'=>$data['kpi_code'],'nik'=>$data['nik'],'kpi_month'=>$data['kpi_month'])); 
   
    }
    
    function approveTarget($data){        
        $dblokal=$this->load->database("default", TRUE);
        foreach($data as $m=>$key){
            $this->db->update('t_trx_target_monthly', $key, array('target_id'=>$key['target_id'])); 
        }
    }
 }